//
//  CreateClassViewController.m
//  TeacherPro
//
//  Created by DCQ on 2017/5/10.
//  Copyright © 2017年 ZNXZ. All rights reserved.
//

#import "ChooseClassViewController.h"
#import "CreateClassView.h"
#import "LeftGradeView.h"
#import "RightClassView.h"
#import "ClassManageModel.h"
 


#define bottomViewHeight     FITSCALE(50)
#define titleLabelTag       10000
@interface ChooseClassViewController ()
@property(nonatomic, strong) UIView * bottomView;
@property(nonatomic, strong) ClassManageListModel *classListModel;
@property(nonatomic, strong) CreateClassView * classListView;
@property(nonatomic, copy)  NSString * gradeId;
@property(nonatomic, copy)  NSString * gradeName;
@property(nonatomic, strong) NSDictionary  *gradeDic;
@property(nonatomic, strong) NSArray  *gradeList;
@property(nonatomic, assign) ViewControllerFromeType  vcType;
@property(nonatomic, strong) NSDictionary *classInfo;
@end

@implementation ChooseClassViewController
- (instancetype)initWithViewControllerFromeType:(ViewControllerFromeType )type{

    if (self ==[super init]) {
        self.vcType = type;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString * title =@"";
    if (self.vcType == ViewControllerFromeType_Choose){
       title = @"选择班级";
    }else if (self.vcType == ViewControllerFromeType_checkChoose){
       title = @"选择班级";
    }
    
    [self setNavigationItemTitle:title];
    
    [self.view addSubview: self.classListView];
    
    self.gradeList = @[@"无年级",@"一年级",@"二年级",@"三年级",@"四年级",@"五年级",@"六年级"];
   
    
    self.gradeDic =@{self.gradeList[1]:@"1",self.gradeList[2]:@"2",self.gradeList[3]:@"3",self.gradeList[4]:@"4",self.gradeList[5]:@"5",self.gradeList[6]:@"6"};

    
    WEAKSELF
//    self.classListView.createBlock = ^(NSString * gradeName){
//        STRONGSELF
//        
//        NSString * gradeID = strongSelf.gradeDic[gradeName];
//        strongSelf.gradeId = gradeID;
//        strongSelf.gradeName = gradeName;
//        
//    };
  
    self.classListView.chooseBlock = ^(NSDictionary *classInfo) {
      STRONGSELF
        strongSelf.classInfo = classInfo;
    };
    self.classListView.checkChooseBlock = ^(ClassManageModel *classInfo) {
     STRONGSELF
        if ([strongSelf.chooseDelegate respondsToSelector:@selector(checkChooseClassInfo:)]) {
            [strongSelf.chooseDelegate checkChooseClassInfo:classInfo];
            [strongSelf backViewController];
        }
    };
    
    [self.view addSubview:self.bottomView];
    [self requestBindingClass];
    
  
    
}

- (CreateClassView *)classListView{
    if (!_classListView) {
        ClassViewFromType type = ClassViewFromType_normal ;
      
       if (self.vcType == ViewControllerFromeType_Choose){
            
          type = ClassViewFromType_choose;
        }
        else if (self.vcType == ViewControllerFromeType_checkChoose){
            
             type = ClassViewFromType_checkChoose;
        }
        _classListView = [[CreateClassView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-bottomViewHeight) withType:type];
    }
    return _classListView;
}
- (UIView *)bottomView{
    if (!_bottomView) {
       
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height -bottomViewHeight,self.view.frame.size.width, bottomViewHeight)];
      
        
     if (self.vcType == ViewControllerFromeType_Choose){
            UIButton * sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [sureBtn  setFrame:CGRectMake(10, 5, _bottomView.frame.size.width- 20, bottomViewHeight-10 )];
            [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
            sureBtn.titleLabel.font = fontSize_15;
            [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [sureBtn setImage:[UIImage imageNamed:@"homework_class_choose"] forState:UIControlStateNormal];
            [sureBtn setImage:[UIImage imageNamed:@"homework_class_choose"] forState:UIControlStateHighlighted];
            [sureBtn setImage:[UIImage imageNamed:@"homework_class_choose"] forState:UIControlStateSelected];
            [sureBtn setBackgroundColor:project_main_blue];
            sureBtn.layer.borderWidth = 1;
            sureBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
            sureBtn.layer.borderColor = project_main_blue.CGColor;
            sureBtn.layer.masksToBounds = YES;
            sureBtn.layer.cornerRadius = (bottomViewHeight-10)/2;
            [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
            
            [_bottomView addSubview:sureBtn];
            [_bottomView setBackgroundColor: [UIColor whiteColor]];
        }else if (self.vcType == ViewControllerFromeType_checkChoose){
            
            UIButton * sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [sureBtn  setFrame:CGRectMake(10, 5, _bottomView.frame.size.width- 20, bottomViewHeight-10 )];
            [sureBtn setTitle:@"查看所有班级" forState:UIControlStateNormal];
            sureBtn.titleLabel.font = fontSize_15;
            [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            [sureBtn setBackgroundColor:project_main_blue];
            sureBtn.layer.borderWidth = 1;
            sureBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
            sureBtn.layer.borderColor = project_main_blue.CGColor;
            sureBtn.layer.masksToBounds = YES;
            sureBtn.layer.cornerRadius = (bottomViewHeight-10)/2;
            [sureBtn addTarget:self action:@selector(checkAllClassAction) forControlEvents:UIControlEventTouchUpInside];
            
            [_bottomView addSubview:sureBtn];
            [_bottomView setBackgroundColor: [UIColor whiteColor]];
        }
        
    }
    return _bottomView;
}

- (BOOL )getLayoutIncludesOpaqueBars{
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ---
- (void)requestBindingClass{
    
    [self sendHeaderRequest:[NetRequestAPIManager getRequestURLStr:NetRequestType_QueryTeacherBindClazzs] parameterDic:nil requestMethodType:RequestMethodType_POST requestTag:NetRequestType_QueryTeacherBindClazzs];
}
-  (void)setNetworkRequestStatusBlocks{
    WEAKSELF
    [self setNetSuccessBlock:^(NetRequest *request, id successInfoObj) {
        STRONGSELF
        if (request.tag == NetRequestType_QueryTeacherBindClazzs) {
            
            strongSelf.classListModel = [[ClassManageListModel alloc]initWithDictionary:successInfoObj error:nil];
            [strongSelf.classListView reloadData:strongSelf.classListModel];
          
            NSMutableArray * manageList = [NSMutableArray array];
            for (NSDictionary * dic in successInfoObj[@"clazzList"]) {
                //adminTeacher : Integer - 是否是班级管理员 0=不是 1=是
                if ([dic[@"adminTeacher"] integerValue] == 1) {
                    [manageList addObject:dic];
                }
            }
            
            if ([strongSelf.classListModel.clazzList count]>0) {
                ClassManageModel*classModel  = strongSelf.classListModel.clazzList[0];
                strongSelf.gradeName = classModel.gradeName;
                NSString * gradeID = strongSelf.gradeDic[classModel.gradeName];
                strongSelf.gradeId = gradeID;
            }else{
                strongSelf.classListView.gradeName = strongSelf.gradeList[1];
                
            }
            
            
            UILabel * titleLabel = [strongSelf.bottomView viewWithTag:titleLabelTag];
            if ([self.classListModel.clazzList count]>0) {
               titleLabel.text =  [NSString stringWithFormat:@"您管理了%zd个班，加入了%zd个班\n共%zd个班",manageList.count,strongSelf.classListModel.clazzList.count-manageList.count,strongSelf.classListModel.clazzList.count];
//                titleLabel.text = @"请选择执教年级创建";
            }else{
                 titleLabel.text = @"请选择执教年级创建";
            }
            
            
        }
        
    }];
}





//
- (void)sureAction{

    [self backViewController];
  
    if ([self.chooseDelegate respondsToSelector:@selector(chooseClassInfo:)]) {
        if (self.classInfo) {
             [self.chooseDelegate chooseClassInfo:self.classInfo];
        }
       
    }
}

- (void)checkAllClassAction{
    [self backViewController];
    if ([self.chooseDelegate respondsToSelector:@selector(checkChooseClassInfo:)]) {
        [self.chooseDelegate checkChooseClassInfo:nil];
    }
}

- (void)dealloc{
    [self setChooseDelegate:   nil];
    [self setBottomView:nil ];
    [self setClassInfo:nil ];
    [self setClassListModel:nil];
    [self setClassListView:nil];
    [self setGradeId:nil];
    [self setGradeDic:nil];
    [self setGradeName: nil];
    [self setGradeList:nil];
    [self clearDelegate];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
